package modules.dimensioning.solution;

import java.util.SortedSet;
import java.util.TreeSet;

import com.vividsolutions.jts.geom.Geometry;

import datatypes.Antenna;
import datatypes.TerrainType;

public class SplitSolutions {

	public PartialAntennaArray[] generatePartialSolutions(Antenna[] antennas, Geometry original, int xparts, int yparts) {

		SortedSet<Double> set = new TreeSet<Double>();		
		for (int i = 0; i < antennas.length; i++) {
			if (antennas[i] == null) 
				continue;
			set.add(antennas[i].getX());
		}
		Double[] xarr = set.toArray(new Double[0]);
		
		

		
		
		PartialAntennaArray[] ps = new PartialAntennaArray[xparts*yparts];
		
		
		for (int x = 0; x < xparts; x++) {
			set = new TreeSet<Double>();;		
			for (int i = 0; i < antennas.length; i++) {
				if (antennas[i] == null) 
					continue;
				if (antennas[i].getX() >= xarr[x*(xarr.length/3)] && antennas[i].getX() <= xarr[(x+1)*(xarr.length/3)-1])
					set.add(antennas[i].getY());
			}		
			Double[] yarr = set.toArray(new Double[0]);
			
			for (int y = 0; y < yparts; y++) {
				//lastminute hack om terraintype in te stellen
				//TODO WEGDOEN!!
				TerrainType tp = TerrainType.SUBURBAN;
				if (x == 1 && y == 1) tp = TerrainType.URBAN;
				
				ps[x*xparts + y] = new PartialAntennaArray(original,tp);
				ps[x*xparts + y].fromX = xarr[x*(xarr.length/3)];
				ps[x*xparts + y].toX = xarr[(x+1)*(xarr.length/3)-1];

				ps[x*xparts + y].fromY = yarr[y*(yarr.length/3)];
				ps[x*xparts + y].toY = yarr[(y+1)*(yarr.length/3)-1];
			}
		}
		for (int i = 0; i < antennas.length; i++) {
			if (antennas[i] == null) 
				continue;
			for (int j = 0; j < ps.length; j++){
				ps[j].addAntenna(antennas[i]);
			}
		}

		return ps;
	}
	

}
